#include "os_obj.h"
#include "bsp.h"

#include "ff.h"
#include "sys_init.h"
#include "common.h"

FIL fil;

void pdf_author_demo(void);
int32_t SDH_Open_Disk(SDH_T *sdh, uint32_t u32CardDetSrc);

TCHAR       sd_path[] = { '1', ':', 0 };    /* SD drive started from 0 */

 const char sd_txt[40] OBJ_ALIGN(8)= "m484 sdcard file test !\n";

UINT bk_len;
uint8_t sd_test_ok;
FRESULT fst;



void sd_test(void)
{
	SDH_Open_Disk(SDH1, CardDetect_From_GPIO);
	
	if(SDH_CardDetection(SDH1))
	{
		char f_op = 0;
		int wrcnt = 1000;
		
		while(!sd_test_ok)
		{
			// path
			f_chdrive(sd_path);          
			
			fst = f_open(&fil, "m484evl.pdf", FA_OPEN_ALWAYS | FA_WRITE);
			
			if(fst != FR_OK) break;
			
			pdf_author_demo();
			
			f_op = 1;
			
			if(fst == FR_OK) file_dbg_puts("sd file write sucess !\n");
			else file_dbg_puts("sd file write fail !\n");
			
			sd_test_ok = 1;
		}
		
		if(f_op) f_close(&fil);
		
	}
	
	SDH_Close_Disk(SDH1);
}

int sd_spdf_disk_out(uint8_t *buf, int len)
{
	UINT FsResdLen;
	
	fst = f_write(&fil, buf, len, &FsResdLen);
	
	return len;
}
